package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.nf.health.app.R;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class BrokenLine extends View {

	private List<Float> data;
	private int yCount;
	private float max, min, region;
	private float mWidth, mHeight;
	private float marginTop, marginBottom, marginAxis;
	private float xInterval, yInterval;
	private float margin_bottom;
	private float interval_left;
	private int goal;
	private float tb;
	private float dot_radius;
	private Paint paint_date, paint_brokenLine, paint_dot, paint_dottedline,
			framPanint, paintLine;

	private Path path;
	private float dotted_text; // 折点值

	// private Bitmap bitmap_point;

	public float getDotted_text() {
		return dotted_text;
	}

	public void setDotted_text(float dotted_text) {
		this.dotted_text = dotted_text;
	}

	private int fineLineColor = 0x5faaaaaa; // 灰色
	private int blueLineColor = 0xff00ffff; // 蓝色
	private int orangeLineColor = 0xaad56f2b; // 橙色
	private int dotColor = 0xffff0000; // 红色
	private List<String> xValues;

	public BrokenLine(Context context, List<Float> data, float max, float min,
						final int yCount) {
		super(context);

		init(data, max, min, yCount);

	}

	private void init(List<Float> data, float max, float min, final int yCount) {
		this.data = data;
		this.max = max;
		this.min = min;
		this.region = max - min;
		this.yCount = yCount;
		marginTop = (int) getResources().getDimension(R.dimen.broken_line_margin_top);
		marginAxis = (int) getResources().getDimension(R.dimen.broken_line_margin_xaxis);
		marginBottom = (int) getResources().getDimension(R.dimen.broken_line_ymargin);
		dot_radius = (int) getResources().getDimension(R.dimen.broken_line_dot_radius);

		getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						mWidth = getWidth();
						mHeight = getHeight();
						yInterval = (mHeight - marginBottom) / yCount;
						getViewTreeObserver().removeGlobalOnLayoutListener(this);
					}
				});
		init(data);
	}
	public BrokenLine(Context context, ArrayList<String> xValues, List<Float> data, float max, float min,
						final int yCount, int goal){
		super(context);
		this.xValues = xValues;
		this.goal = goal;
		if(goal>max){
			max = goal*1.05f;
		}
		init(data, max, min, yCount);
	}

	public void init(List<Float> data) {
		if (null == data || data.size() == 0)
			return;
		this.data = data;
		Resources res = getResources();
		tb = res.getDimension(R.dimen.historyscore_tb);
		xInterval = tb * 5.0f;
		interval_left = tb * 0.5f;
		margin_bottom = 8 * tb * 0.2f;

		paint_date = new Paint();
		paint_date.setStrokeWidth(tb * 0.1f);
		paint_date.setTextSize(tb * 1.2f);
		paint_date.setAntiAlias(true);
		paint_date.setTextAlign(Paint.Align.CENTER);
		paint_date.setColor(Color.GRAY);

		paint_brokenLine = new Paint();
		paint_brokenLine.setStrokeWidth(tb * 0.1f);
		paint_brokenLine.setColor(getResources().getColor(R.color.theme_color));
		paint_brokenLine.setAntiAlias(true);

		paintLine = new Paint();
		paintLine.setStrokeWidth(tb * 0.1f);
		paintLine.setColor(orangeLineColor);
		paintLine.setAntiAlias(true);

		paint_dot = new Paint();
		paint_dot.setStrokeWidth(tb * 0.1f);
		paint_dot.setColor(getResources().getColor(R.color.theme_color));
		paint_dot.setAntiAlias(true);

		paint_dottedline = new Paint();
		paint_dottedline.setStyle(Paint.Style.STROKE);
		paint_dottedline.setColor(getResources().getColor(R.color.theme_color));

		framPanint = new Paint();
		framPanint.setAntiAlias(true);
		framPanint.setStrokeWidth(2f);
		framPanint.setColor(Color.TRANSPARENT);

		path = new Path();
		// bitmap_point = BitmapFactory.decodeResource(getResources(),
		// R.drawable.dot_red);
		setLayoutParams(new LayoutParams((int) (this.data.size() * xInterval),
				LayoutParams.MATCH_PARENT));
	}
	@Override
	protected void onDraw(Canvas canvas) {
		if (null == data || data.size() == 0)
			return;
		drawBrokenLine(canvas);
		drawDate(canvas);

		super.onDraw(canvas);
	}

	/**
	 * 绘制折线
	 *
	 * @param c
	 */
	public void drawBrokenLine(Canvas c) {
//		float base = getHeight() - marginTop - marginBottom;
		float base = getHeight() - marginBottom;
		float startX = 0.0F;
		float startY = 0.0F;

//		Shader mShader = new LinearGradient(0, 0, 0, getHeight(), new int[] {
//				Color.argb(100, 0, 255, 255), Color.argb(45, 0, 255, 255),
//				Color.argb(10, 0, 255, 255) }, null, Shader.TileMode.CLAMP);
//		framPanint.setShader(mShader);
		if(data.size()==1){
			float x1 = xInterval / 2;
//			float y1 = marginTop + base - (base * (data.get(i) - min) / region);
			float y1 = base - (base * (data.get(0) - min) / region) + marginTop;
			c.drawCircle(x1, y1, dot_radius, paint_dot);
		}

		for (int i = 0; i < data.size()-1; i++) {
			float x1 = xInterval / 2 + xInterval * i;
//			float y1 = marginTop + base - (base * (data.get(i) - min) / region);
			float y1 = base - (base * (data.get(i) - min) / region) + marginTop;
			float x2 = xInterval / 2 + xInterval * (i + 1);
//			float y2 = marginTop + base
//					- (base * (data.get(i + 1) - min) / region);
			float y2 = base
					- (base * (data.get(i + 1) - min) / region) + marginTop;

			if (i == 0) {
				startX = x1;
				startY = y1;
				path.moveTo(startX, startY);
			}

			c.drawLine(x1, y1, x2, y2, paint_brokenLine);
			path.lineTo(x1, y1);

			// c.drawBitmap(bitmap_point, x1 - bitmap_point.getWidth() / 2, y1 -
			// bitmap_point.getHeight() / 2, null);
			c.drawCircle(x1, y1, dot_radius, paint_dot);
			if (i == data.size() - 2) {
				path.lineTo(x2, y2);
				path.lineTo(x2, getHeight() - marginBottom + marginTop);
				path.lineTo(startX, getHeight() - marginBottom + marginTop);
				path.close();
				c.drawPath(path, framPanint);
				// c.drawBitmap(bitmap_point, x2 - bitmap_point.getWidth() / 2,
				// y2 - bitmap_point.getHeight() / 2, null);
				c.drawCircle(x2, y2, dot_radius, paint_dot);
			}
		}
		if(goal != 0){
//			float y1 = base - (base * (float)(goal) / region) + marginTop;
			float y1 = base - (base * (float)(goal-min) / region) + marginTop;//修改目标线height,以前的若region<goal将出错
			c.drawLine(0, y1, getWidth(), y1, paintLine);
			paint_date.setTextSize(tb * 1f);
			c.drawText("目标:" + goal, DensityUtil.dip2px(getContext(), 30), y1-6, paint_date);
		}
	}

	/**
	 * 绘制时间
	 *
	 * @param c
	 */
	public void drawDate(Canvas c) {
		// Date date=new Date();//取时间
		// Calendar calendar =new GregorianCalendar();
		// calendar.setTime(date);
		// SimpleDateFormat dateFormat=new SimpleDateFormat("MM-dd");
		// String[] dates=new String[data.size()];
		// for (int i = 0; i < data.size(); i++) {
		// calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
		// date=calendar.getTime(); //这个时间就是日期往后推一天的结果
		// dates[i]=dateFormat.format(date).toString();
		// }
		if(goal != 0){
			for (int i = 0; i < xValues.size(); i++) {
//				String[] times = xValues.get(i).split(" ");
//				String[] date = times[0].split("-");
//				c.drawText(date[1]+"月"+date[2]+"日", xInterval / 2 + xInterval * i,
//						getHeight() - marginBottom + marginAxis + marginTop, paint_date);
				String text = DateUtil.date2String(Long.parseLong(xValues.get(i)), "MM.dd");
				paint_date.setTextSize(tb * 1.2f);
				paint_date.setColor(Color.BLACK);
				c.drawText(text, xInterval / 2 + xInterval * i, getHeight() - marginBottom + marginAxis + marginTop, paint_date);
			}
		}else{
			for (int i = 0; i < data.size(); i++) {
				c.drawText("第" + (i + 1) + "次", xInterval / 2 + xInterval * i,
						getHeight() - marginBottom + marginAxis + marginTop, paint_date);
			}
		}
	}
}